//
//  GameListCell.swift
//  Game
//
//  Created by woniu on 2025/9/1.
//

import Foundation
import UIKit

// MARK: - GameListCell
class GameListCell: UITableViewCell {
    
    private let indexLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 9, weight: .medium)
        label.textColor = .black
        label.textAlignment = .center
        label.numberOfLines = 1
        return label
    }()
    
    private let iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.tintColor = .black
        return imageView
    }()
    
    private let nameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        label.textColor = .black
        label.numberOfLines = 1
        return label
    }()
    
    private let descriptionLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        label.textColor = UIColor.gray
        label.numberOfLines = 2
        return label
    }()
    
    public let dateLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        label.textColor = UIColor.gray
        label.numberOfLines = 2
        label.textAlignment = .right
        return label
    }()
    
    private let arrowImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(systemName: "chevron.right")
        imageView.contentMode = .scaleAspectFit
        imageView.tintColor = UIColor.gray
        return imageView
    }()
    
    // MARK: - Initialization
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Setup Methods
    private func setupUI() {
        backgroundColor = .white
        
        contentView.addSubview(iconImageView)
        contentView.addSubview(indexLabel)
        contentView.addSubview(nameLabel)
        contentView.addSubview(descriptionLabel)
        contentView.addSubview(arrowImageView)
        contentView.addSubview(dateLabel)
       
        arrowImageView.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-20)
            make.centerY.equalToSuperview()
            make.size.equalTo(CGSize(width: 20, height: 20))
        }
        
        iconImageView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(35)
            make.centerY.equalToSuperview()
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        indexLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.left).offset(-8)
            make.top.equalTo(iconImageView.snp.top)
            make.size.equalTo(CGSize(width: 16, height: 16))
        }
        
        dateLabel.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.right.equalTo(arrowImageView.snp.left).offset(-10)
            make.width.equalTo(190)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(15)
            make.top.equalToSuperview().offset(15)
            make.right.equalTo(dateLabel.snp.left).offset(-10)
        }
        
        descriptionLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(5)
            make.right.equalTo(nameLabel)
            make.bottom.lessThanOrEqualToSuperview().offset(-15)
        }
        
       
    }
    
    func updateBy(model item: GameModel,icon:String,index:Int) {
//        indexLabel.text = "\(index+1)"
        iconImageView.image = UIImage(systemName:icon)
        nameLabel.text = item.name
        descriptionLabel.text = item.type
        let dateStr = item.key
        if dateStr.count >= 14{
            let year = dateStr.subString(from: 0, to: 3)
            let month = dateStr.subString(from: 4, to: 5)
            let day = dateStr.subString(from: 6, to: 7)
            let hh = dateStr.subString(from: 8, to: 9)
            let mm = dateStr.subString(from: 10, to: 11)
            let ss = dateStr.subString(from: 12, to: 13)
            let dateStr = "\(year)-\(month)-\(day) \(hh):\(mm):\(ss)"
            dateLabel.text = DateUtil.getCHString(dateTimeStr: dateStr)
        }
        
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        iconImageView.image = nil
        nameLabel.text = nil
        descriptionLabel.text = nil
    }
}
